import 'dart:async';

import 'package:common/common.dart';
import 'package:get/get.dart';

import '../../tabbar_page/tabbar_page.dart';
import '../edit_info/user_info_edit_sex_page.dart';
import '../model/login_modle.dart';
import '../user/user.dart';
import 'login_phone_state.dart';

class LoginPhoneLogic extends GetxController {
  final LoginPhoneState state = LoginPhoneState();

  bool pwdVisibility = false; //是否明文展示密码
  bool isChecked = false; //是否勾选协议

  String verificationKey = "";
  // 切换隐藏密码框
  void switchPwdVisibility() {
    pwdVisibility = !pwdVisibility;
    update();
  }

  // 勾选协议
  void handlerSelectAgreeCheckbox() {
    isChecked = !isChecked;
    update();
  }

  /// 执行注册逻辑
  void doPhoneCodeLogin() async {
    update();

    logs('phone:${state.phone} code:${state.code}  ', tag: '登录注册');

    if (!isChecked) {
      Toast.showText('请先阅读并同意协议'.tr);
      return;
    }
    if (state.phone.toString().checkEmpty) {
      Toast.showText('手机号码不能为空'.tr);
      return;
    }
    if (state.code.toString().checkEmpty) {
      Toast.showText('请输入6位验证码'.tr);
      return;
    }

    SmartDialog.showLoading();

    UserService.to.removeToken();

    Result result = await HttpProvider.post('/socials/phone/authorizationssocials',
        params: {
          'verification_key': verificationKey,
          'verification_code': state.code,
          'reg_id': 'asdfasfadsfsafadsfasdfdas',
        },
        explain: '登录');
    SmartDialog.dismiss();
    if (result.succ) {
      LoginModel model = LoginModel.fromJson(result.data);
      UserService.to.saveLoginModel(login: model);
      if (model.initStep == 'init') {
        Get.to(const UserInfoEditSexPage());
      } else {
        Get.offAll(const TabBarPage());
      }

      doJumpMainApp(
        data: result.data,
      );
    } else {
      Toast.showText(result.message);
    }
  }

  void doJumpMainApp({required Map<String, dynamic> data}) {
    // String token = data['token'];

    // bool isFirstLogin = data['is_first_login'];
    // logs('$isFirstLogin');

    // UserService.to.setToken(token);
    // jPushReceive.init(); // 初始化极光推送

    // //发送通知请求用户详情数据
    // bus.post(eventName: EventBudKeys.eventProfile2Refresh, data: true);
    // final MineLogic logic = Get.put(MineLogic());
    // logic.getOrderCount();
    // //使用SingleTask启动模式去首页
    // // AppMainPage.startInstance();

    // if (isBack) {
    //   Get.pop(); //关闭当前页面
    // } else {
    //   AppMainPage.startInstance();
    // }
  }

  /// 默认的App Dialog
  // void showVerifyCodedDialog() async {
  //   if (!isChecked) {
  //     Toast.showText('请先阅读并同意协议'.tr);
  //     return;
  //   }
  //   //校验手机号码不能为空
  //   if (state.phone.toString().checkEmpty) {
  //     Toast.showText('手机号码不能为空'.tr);
  //     return;
  //   }
  //   if (state.phone.length != 11 ||
  //       state.phone.startsWith('10') ||
  //       state.phone.startsWith('11') ||
  //       state.phone.startsWith('12')) {
  //     Toast.showText('请输入正确的手机号码'.tr);
  //     return;
  //   } else {
  //     Result result = await HttpProvider.post('/socials/phone/authorizationssocials',
  //         params: {
  //           'verification_key': state.phone,
  //           'verification_code': state.phone,
  //           'reg_id': '极光推送设备码',
  //         },
  //         explain: '登录');
  //     Toast.showText(result.message);
  //     if (result.succ) {
  //       //成功之后开始倒计时
  //       _startCountDown();
  //     }
  //   }
  // }

  /// 请求发送验证码接口
  Future<bool> requestSendVerifyCode() async {
    UserService.to.removeToken();

    Result result = await HttpProvider.post('/verificationCodes',
        params: {
          'codekind': 'join', // join 注册登录   leave 注销
          'phone': state.phone,
        },
        explain: '发送验证码');
    if (result.succ) {
      verificationKey = result.data['key'];
      return true;
    }
    return false;
  }
}
